home *** CD-ROM | disk | FTP | other *** search
/ PC Shareware 1997 February / PC Shareware 1997-02.iso / programy / e! / api / ewfext.c_ / ewfext.C
Encoding:
C/C++ Source or Header  |  1995-03-05  |  6.1 KB  |  199 lines

  1. /*----------------------------------------------------------------
  2.    EWFEXT.C -- Extension DLL for E! and File Manager - version 2.1
  3.  
  4.    To compile: nmake /f ewfext.mak
  5.  
  6.    Please use the version of WFEXT.H coming with the W4FW SDK.
  7.  
  8.    Once compiled, copy EWFEXT.DLL to any directory of your choice.
  9.  
  10.    This DLL is not like any other Extension DLL for E!. It's a
  11.    standard Extension DLL for the Windows File Manager. It simply
  12.    adds an EW item to the File Manager menu, allowing you to load
  13.    files into E! even if no association is valid for this file.
  14.  
  15.    To install it, you just have to add the following line to the
  16.    [addons] section fo WINFILE.INI:
  17.  
  18.    EW=c:\ew\ewfext.dll
  19.  
  20.    You can put this file in any directory.
  21.  
  22.    If E! is not running when you select the Edit item in this menu,
  23.    it will be launched first and then the selected file(s) will be
  24.    loaded.
  25.  
  26.    However, E! will not be able to load itself if the \EW directory
  27.    has not been added to your PATH. Normally, \EW doesn't need to be
  28.    added to your PATH but in such a case, it's mandatory. Please see
  29.    technical note #11.
  30.  
  31.   --------------------------------------------------------------*/
  32. #include <windows.h>
  33.  
  34. #define WINBALL
  35.  
  36. #include <\wfwdev\include\wfext.h>
  37. #include "ewfext.h"
  38.  
  39. /* type definition for the some API functions.          */
  40. /* Note that we have not linked EWAPI2.LIB to this DLL.   */
  41. /* Otherwise, E! would be systematically loaded each time */
  42. /* we'd try to run File Manager.                          */
  43.  
  44. typedef void (FAR PASCAL* EWEditFileProc)(char FAR* Name);
  45. typedef unsigned int (FAR PASCAL* EWGetVersionProc) (void);
  46. typedef int (FAR PASCAL* EWMessageBoxProc) (HWND WndParent, LPCSTR Txt, LPCSTR Caption, UINT TextType);
  47.  
  48. HINSTANCE     hinst;
  49. HMENU         hmenu;
  50. char         EwApiName[11];
  51. HMODULE      hEWLib = 0;
  52. EWEditFileProc     EWAsynchEditFile;
  53. EWGetVersionProc EWGetVersion;
  54. EWMessageBoxProc EWMessageBox;
  55.  
  56. EXT_BUTTON ewbutton[1] = {{ IDM_EDIT, 0, FMTB_BUTTON }}; // button identifier is 1
  57.  
  58. int FAR PASCAL LibMain (HANDLE hInstance, WORD wDataSeg, WORD wHeapSize,
  59.             LPSTR lpszCmdLine)
  60. {
  61.   if (wHeapSize > 0)
  62.     UnlockData (0) ;
  63.   hinst = hInstance;
  64.   lstrcpy(EwApiName, "ewapi2.dll");
  65.   return 1 ;
  66. }
  67.  
  68. int FAR PASCAL _export _WEP(int nParameter)
  69.  
  70. {
  71.   if (hEWLib >= 32)
  72.     FreeLibrary(hEWLib);
  73.   return(1);
  74. }
  75.  
  76. HMENU FAR PASCAL _export FMExtensionProc (HWND hwnd, WORD wMsg, LONG lParam)
  77. {
  78.     int count;
  79.  
  80.     switch (wMsg) {
  81.     case FMEVENT_LOAD:
  82.  
  83.         #define lpload  ((LPFMS_LOAD)lParam)
  84.  
  85.         /* Fill the FMS_LOAD structure. */
  86.         lpload->dwSize = sizeof(FMS_LOAD);
  87.         lstrcpy(lpload->szMenuName, "&EW");
  88.  
  89.         /* Return the handle of the menu. */
  90.         return (DWORD) (lpload->hMenu = LoadMenu(hinst, "EWMENU"));
  91.  
  92.     case FMEVENT_INITMENU:
  93.  
  94.         hmenu = (HMENU) HIWORD(lParam);
  95.         return (hmenu);
  96.  
  97.     case FMEVENT_TOOLBARLOAD:
  98.         {
  99.         LPFMS_TOOLBARLOAD ps;
  100.  
  101.         ps = (LPFMS_TOOLBARLOAD)lParam;
  102.         ps->dwSize = sizeof(FMS_TOOLBARLOAD);
  103.         ps->lpButtons = (LPEXT_BUTTON)ewbutton;
  104.         ps->cButtons = 1;        // two buttons to add
  105.         ps->cBitmaps = 1;        // two images in bitmap
  106.         ps->idBitmap = IDBM_EDIT;   // bitmap resource 1
  107.         ps->hBitmap = NULL;        // not a memory bitmap
  108.         }
  109.         break;
  110.  
  111.     case FMEVENT_HELPSTRING:
  112.         {
  113.         LPFMS_HELPSTRING ps;
  114.  
  115.         ps = (LPFMS_HELPSTRING)lParam;
  116.         switch (ps->idCommand) {
  117.         case IDM_EDIT:
  118.             lstrcpy(ps->szHelp, "Edit File");
  119.             break;
  120.         }
  121.         }
  122.         break;
  123.  
  124.     case IDM_EDIT:
  125.       {
  126.         BOOL   fLFN;
  127.         static FMS_GETFILESEL file;
  128.         UINT   nVersion = 0;
  129.  
  130.         if ((hEWLib = GetModuleHandle(EwApiName)) == 0) /* EWAPI2.DLL is not loaded */
  131.           if ((hEWLib = LoadLibrary(EwApiName)) < 32)
  132.           /* Can't load it */
  133.         break;
  134.  
  135.         EWGetVersion = (EWGetVersionProc) GetProcAddress(hEWLib, "EWGetVersion");
  136.         EWMessageBox = (EWMessageBoxProc) GetProcAddress(hEWLib, "EWMessageBox");
  137.  
  138.         if (!EWGetVersion || !EWMessageBox)
  139.           break;
  140.  
  141.         // Check version number
  142.         nVersion = EWGetVersion();
  143.         if ((HIBYTE(nVersion) < 2) || ((HIBYTE(nVersion) == 2) && (LOBYTE(nVersion) < 2)))
  144.         {
  145.           EWMessageBox(GetFocus(),
  146.                "This Extension DLL needs E! version 2.02 or newer.",
  147.                "Error!",
  148.                MB_OK | MB_ICONEXCLAMATION);
  149.           break;
  150.         }
  151.  
  152.         count = (int) SendMessage(hwnd,
  153.                       fLFN ? FM_GETSELCOUNTLFN : FM_GETSELCOUNT,
  154.                       FMFOCUS_DIR,
  155.                       0L);
  156.  
  157.  
  158.         while (count >= 1)
  159.           {
  160.         /* Selection indices are zero-based (0 is first). */
  161.         count--;
  162.         SendMessage(hwnd,
  163.                 FM_GETFILESEL,
  164.                 count,
  165.                 (LONG) (LPFMS_GETFILESEL)&file);
  166.         OemToAnsi(file.szName, file.szName);
  167.  
  168.         /* Edit File */
  169.         /*
  170.           A few explanations are in order here.
  171.           When loading a file E! checks whether the file is a text file and displays
  172.           a warning if it seems not to be the case. However, when displaying the error
  173.           message box and if editing has been triggered from the File Manager, mouse
  174.           and keyboard messages will not be forwarded to the dialog box. This is
  175.           because we are currently processing a message sent to the File Manager.
  176.           To circumvent this problem, we have added a new function to the API.
  177.           EWAsynchEditFile doesn't wait for an error code to be returned. So,
  178.           the messages that are sent to the application queue of E! can therefore be
  179.           processed. Actually, EWAsynchEditFile does the same job as EWEditFile but
  180.           the API uses PostMessage instead of SendMEssage to signal the request to E!.
  181.  
  182.           CAUTION: Using EWAsynchEditFile requires some special handling. Because we will
  183.           immediately exit from FMExtensionProc, we can't allocate the "file" variable
  184.           on the stack. Otherwise, when E! will receive the edit request from the API,
  185.           the pointer to the filename will no longer be valid. Thus, it's mandatory to
  186.           use a static variable to store the FMS_GETFILESEL structure.
  187.         */
  188.         EWAsynchEditFile = (EWEditFileProc) GetProcAddress(hEWLib, "EWAsynchEditFile");
  189.         if (!EWAsynchEditFile)
  190.           break;
  191.         EWAsynchEditFile((char FAR*) file.szName);
  192.           }
  193.  
  194.         break;
  195.       }
  196.     }
  197.     return NULL;
  198. }
  199.